package com.ztr.test;

import java.io.IOException;
import java.nio.file.*;  
import java.util.*;  
  
public class FolderWatcherWithImageComparison {  
  
    private static final String WATCH_DIRECTORY = "D:\\1";  
    private static final String TARGET_DIRECTORY = "D:\\yunqu\\tf-access\\images\\BasicFace";
  
    public static void main(String[] args) throws IOException, InterruptedException {  
        WatchService watchService = FileSystems.getDefault().newWatchService();  
        Path directoryToWatch = Paths.get(WATCH_DIRECTORY);  
        directoryToWatch.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);  
  
        while (true) {  
            WatchKey key = watchService.take();  
            for (WatchEvent<?> event : key.pollEvents()) {  
                WatchEvent.Kind<?> kind = event.kind();  
                if (kind == StandardWatchEventKinds.ENTRY_CREATE) {  
                    WatchEvent<Path> ev = (WatchEvent<Path>) event;  
                    Path fileName = ev.context();  
                    Path newFolderPath = directoryToWatch.resolve(fileName);  
  
                    if (Files.isDirectory(newFolderPath)) {  
                        System.out.println("新的文件夹被创建: " + newFolderPath);  
                        findMostSimilarImage(newFolderPath, TARGET_DIRECTORY);  
                    }  
                }  
            }  
            boolean valid = key.reset();  
            if (!valid) {  
                break;  
            }  
        }  
    }  
  
    private static void findMostSimilarImage(Path newFolderPath, String targetFolderPath) throws IOException {  
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(newFolderPath, "*.{jpg,png,jpeg}")) {  
            for (Path imagePath : directoryStream) {  
                String imageName = imagePath.getFileName().toString();  
                System.out.println("找到图片: " + imageName);  
  
                // 这里应该使用图像处理库来比较图片相似度，但为简化示例，我们仅使用文件名比较  
                Optional<Path> mostSimilar = findMostSimilarImageName(imageName, targetFolderPath);  
                if (mostSimilar.isPresent()) {  
                    System.out.println("相似度最高的图片: " + mostSimilar.get().getFileName());  
                } else {  
                    System.out.println("在目标文件夹中没有找到相似的图片。");  
                }  
            }  
        } catch (NotDirectoryException e) {  
            System.err.println("路径不是一个目录: " + newFolderPath);  
        }  
    }  
  
    private static Optional<Path> findMostSimilarImageName(String imageName, String targetFolderPath) throws IOException {  
        Path targetDir = Paths.get(targetFolderPath);  
        List<String> targetImageNames = new ArrayList<>();  
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(targetDir, "*.{jpg,png,jpeg}")) {  
            for (Path path : directoryStream) {  
                targetImageNames.add(path.getFileName().toString());  
            }  
        }  
  
        // 简化示例：通过文件名比较来模拟相似度  
        Optional<String> mostSimilarName = targetImageNames.stream()  
                .min((name1, name2) -> Math.abs(imageName.compareTo(name1)) - Math.abs(imageName.compareTo(name2)));
  
        return mostSimilarName.map(name -> targetDir.resolve(name));  
    }  
}